<?php
/**
 * This file is lil-wide model file. 
 *
 * @copyright     Copyright 2008-2010, LilCake.net
 * @link          http://lilcake.net LilCake.net
 * @package       lil
 * @subpackage    lil
 * @license       MIT License (http://www.opensource.org/licenses/mit-license.php)
 */
App::import('Lib', 'Lil.LilPluginRegistry');
/**
 * LilAppModel class
 *
 * @uses          AppModel
 * @package       lil
 * @subpackage    lil
 */
class LilAppModel extends AppModel {
/**
 * __construct method
 *
 * @param mixed $id
 * @param mixed $table
 * @param mixed $ds
 * @access private
 * @return void
 */
	function __construct($id = false, $table = null, $ds = null) {
		App::import('Libs', 'Lil.LilPluginRegistry');
		
		if ($table_prefix = Configure::read('Lil.tablePrefix')) {
			$this->tablePrefix = $table_prefix;
		}
		
		$this->callPluginHandlers('before_construct_model');
		parent::__construct($id, $table, $ds);
		$this->callPluginHandlers('after_construct_model');
	}
/**
 * beforeSave method
 *
 * @access public
 * @return boolean
 */
	function beforeSave() {
		$data = $this->callPluginHandlers('before_save_model', array('data' => $this->data));
		if (isset($data['data'])) $this->data = $data['data'];
		if (isset($this->data['return'])) return (boolean)$this->data['return'];
		return true;
	}
/**
 * afterSave method
 *
 * @param boolean $created true if a new object was created
 * @access public
 * @return void
 */
	function afterSave($created = null) {
		$data = $this->callPluginHandlers('after_save_model',
			array('data' => $this->data, 'id' => $this->id, 'created' => $created)
		);
		if (isset($data['data'])) $this->data = $data['data'];
	}
/**
 * beforeFind method
 *
 * @param mixed $queryData
 * @access public
 * @return void
 */
	function beforeFind($queryData = null) {
		$queryData = $this->callPluginHandlers('before_find_model', $queryData);
		return $queryData;
	}
/**
 * isAuthorized method
 *
 * @access public
 * @return void
 */
	function isAuthorized($user, $controller, $action) {
		return $this->callPluginHandlers('isAuthorizedModel', array(
			'user'       => $user,
			'controller' => $controller,
			'action'     => $action
		));
	}
/**
 * callPluginHandlers method
 *
 * @param string $handler
 * @access public
 * @return void
 */
	function callPluginHandlers($handler, $args = null) {
		$ret = $args;
		// execute plugin handlers
		
		$plugins = LilPluginRegistry::keys();
		foreach ((array)$plugins as $plugin_key) {
			$plugin = LilPluginRegistry::getObject($plugin_key);
			
			if (!empty($plugin->handlers[$handler]) &&
			method_exists($plugin, $plugin->handlers[$handler]['function']))
			{
				$ret = call_user_func_array(
					array($plugin, $plugin->handlers[$handler]['function']),
					array(
						'model'  => $this, 
						'args'   => (array)$args,
						'params' => (array)$plugin->handlers[$handler]['params'],
					)
				);
			}
		}
		if (isset($ret)) return $ret;
	}
/**
 * getAdminAreaId method
 *
 * This functions returns current area id in administration.
 *
 * @access public
 * @return int
 */
	function getAdminAreaId() {
		if ($data = $this->getAdminArea()) {
			return $data['Area']['id'];
		} else return null;
	}
/**
 * getAdminAreaSlug method
 *
 * This functions returns current area slug in administration.
 *
 * @access public
 * @return int
 */
	function getAdminAreaSlug() {
		if ($data = $this->getAdminArea()) {
			return $data['Area'][Configure::read('Lil.areaSlugField')];
		} else return null;
	}
/**
 * getAdminArea method
 *
 * This functions returns current area in administration.
 *
 * @access public
 * @return array
 */
	function getAdminArea() {
		static $session = null;
		if (empty($session)) $session = new CakeSession();
		
		if (!empty($session) && ($a = $session->read('admin.area'))) {
			return $a;
		} else {
			return null;
		}
	}
/**
 * user method
 *
 * Returns current user
 *
 * @param mixed $key
 * @access public
 * @return mixed
 */
	function user($key = null) {
		static $session = null;
		if (empty($session)) $session = new CakeSession();
		
		if ($session->valid(Configure::read('Lil.authSessionKey'))) {
			if ($key == null) {
				return array(
					Configure::read('Lil.authModel') => 
						$session->read(Configure::read('Lil.authSessionKey'))
				);
			} else {
				$user = $session->read(Configure::read('Lil.authSessionKey'));
				if (isset($user[$key])) return $user[$key];
			}
		}
		return null;
	}
/**
 * isAdmin method
 *
 * Returns user is an administrator. 
 * If there is no user specified, current user admin status is returned.
 *
 * @param mixed $user_id
 * @access public
 * @return mixed
 */
	function isAdmin($user_id = null) {
		if (empty($user_id) || ($user_id == $this->user('id'))) {
			static $session = null;
			if (empty($session)) $session = new CakeSession();
			
			if ($session->valid(Configure::read('Lil.authSessionKey')) &&
				$user = $session->read(Configure::read('Lil.authSessionKey')))
			{
				return (bool)$user[Configure::read('Lil.userAdminField')];
			}
			return false;
		} else {
			$User =& ClassRegistry::init('Lil.User');
			return (bool)$User->field(
				Configure::read('Lil.userAdminField'),
				array('User.id' => $user_id)
			);
		}
	}
}